//
//  BottomBarView.m
//  GPUImage_MyDemo
//
//  Created by yuedongkui on 2018/5/18.
//  Copyright © 2018年 LY. All rights reserved.
//

#import "BottomBarView.h"
#import "BottomNavBar.h"

static NSString *kCellReuseIdentifier = @"kCellReuseIdentifier";


@interface BottomBarView ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *lists;
@property (nonatomic, strong) BottomNavBar *navBar;

@end



@implementation BottomBarView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
//        self.lists = @[@"亮度", @"饱和度", @"对比度", @"色温", @"模糊", @"高光", @"阴影", @"暗角", @"锐化", @"色阶"];
//        self.lists = @[@"一键美图", @"编辑", @"增强", @"滤镜", @"马赛克", @"涂鸦", @"边框", @"贴纸", @"文字"];
        self.lists = @[@"增强", @"滤镜"];

        UICollectionViewFlowLayout *collectionViewLayout = [[UICollectionViewFlowLayout alloc] init];
        [collectionViewLayout setItemSize:CGSizeMake(kBarCellWidth, kBarCellHeight)];
        [collectionViewLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        collectionViewLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        collectionViewLayout.minimumInteritemSpacing = 0;
        collectionViewLayout.minimumLineSpacing = 0;

        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:collectionViewLayout];
        self.collectionView.backgroundColor = [UIColor whiteColor];
        [self.collectionView registerClass:[BarCell class] forCellWithReuseIdentifier:kCellReuseIdentifier];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        [self addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self).offset(0);
            make.height.mas_equalTo(kBarCellHeight);
        }];

        self.navBar = [[BottomNavBar alloc] initWithFrame:CGRectZero];
        [self addSubview:self.navBar];
        [self.navBar mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self).offset(0);
            make.top.equalTo(self.collectionView.mas_bottom).offset(0);
            make.bottom.equalTo(self);
        }];
    }
    return self;
}

#pragma mark - UICollectionViewDelegate & UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.lists.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    BarCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellReuseIdentifier forIndexPath:indexPath];
    cell.backgroundColor = [UIColor lightGrayColor];
    cell.titleLabel.text = self.lists[indexPath.row];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    BarCell *cell = (BarCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.iconImageView.image = [UIImage imageNamed:@"abc_ic_star_black_48dp_h"];
}

- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    BarCell *cell = (BarCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.iconImageView.image = [UIImage imageNamed:@"abc_ic_star_black_48dp"];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"--- %ld", indexPath.row);
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];

    [self.delegate bottomBarView:self didSelectRow:indexPath.row];
}


@end





@implementation BarCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.iconImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        self.iconImageView.image = [UIImage imageNamed:@"abc_ic_star_black_48dp"];
        [self addSubview:self.iconImageView];
        [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self).offset(0);
            make.size.mas_offset(CGSizeMake(kBarCellWidth, kBarCellHeight-20));
        }];
        
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.titleLabel.textColor = [UIColor blackColor];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.font = [UIFont systemFontOfSize:14];
        [self addSubview:self.titleLabel];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self).offset(0);
            make.top.equalTo(self.iconImageView.mas_bottom);
        }];
    }
    return self;
}

@end








